﻿using System;
using System.Collections.Generic;
using System.Text;
using FDSimpleModelGenerator.Common;
using Microsoft.Extensions.DependencyInjection;

namespace FDSimpleModelGenerator.Cache.Redis
{
    /// <summary>
    /// 使用redis服务
    /// </summary>
    public static class RedisCacheServiceExtensions
    {
        /// <summary>
        /// 使用redis缓存
        /// 在 FDSimpleModelGenerator.Common.AddCommonServices之后调用
        /// </summary>
        /// <param name="services"></param>
        /// <param name="hasRedis">是否有redis</param>
        /// <returns></returns>
        public static IServiceCollection AddRedisCache(this IServiceCollection services, bool hasRedis)//Action<IRedisCacheConfig> options)
        {
            if (!hasRedis)
            {
                return services;
            }
            services.Remove(new ServiceDescriptor(typeof(ICache), f => f.GetService<ICache>(),
                ServiceLifetime.Singleton));
            //var config = new RedisCacheConfig();
            //options.Invoke(config); // 必须配置
            //if (config.ConnectionString.IsNullOrEmpty())
            //    throw new RedisConfigException("redis连接信息必须配置");
            //services.AddSingleton<IRedisCacheConfig>(config);
            services.AddSingleton<ICache, RedisCache>();

            return services;
        }
    }
}
